package com.browan.background;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class Welcome extends Activity
{

	public static final String POSTION = "postion";

	private List<WelComeData> list;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		GridView gridView = new GridView(this);

		gridView.setBackgroundResource(R.drawable.bg);

		gridView.setNumColumns(2);

//		gridView.setPadding(10, 50, 10, 0);
		
//		gridView.setPadding(10, 30, 10, 0);
		
		gridView.setPadding(10, 15, 10, 0);

		gridView.setVerticalSpacing(20);

		gridView.setHorizontalSpacing(10);
		
		gridView.setVerticalFadingEdgeEnabled(true);
		
		gridView.setVerticalScrollBarEnabled(false);

		list = createData();

		GridViewAdapter adapter = new GridViewAdapter(this, list);
		gridView.setAdapter(adapter);

		gridView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				Intent intent = new Intent(Welcome.this, Background.class);
				intent.putExtra(POSTION, list.get(position).index);

				intent.putExtra(Intent.EXTRA_TITLE, list.get(position).name);

				startActivity(intent);
			}
		});

		setContentView(gridView);

		if (!NetWorkUtil.isLive(this))
		{
			Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("提醒");
			builder.setMessage("本应用需要网络支持，没有发现任何网络可用！");
			builder.setPositiveButton("设置", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					setting.dismiss();
					Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
					Welcome.this.finish();
				}
			}).setNegativeButton("退出", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					setting.dismiss();
					Welcome.this.finish();
				}
			});
			builder.setCancelable(false);
			setting = builder.create();
			setting.show();

		}

	}

	AlertDialog setting;

	private List<WelComeData> createData()
	{

		List<WelComeData> list = new ArrayList<WelComeData>();
		list.add(new WelComeData("最新壁纸", StyleManager.LAST_PICTURE));

		list.add(new WelComeData("美女壁纸",StyleManager.BELLE));

		list.add(new WelComeData("恋爱壁纸", StyleManager.LOVE));

		list.add(new WelComeData("可爱壁纸", 3));

		list.add(new WelComeData("摄影壁纸", 23));

		list.add(new WelComeData("动漫壁纸", 17));

		list.add(new WelComeData("动物壁纸", 22));

		list.add(new WelComeData("名车壁纸", 5));

		list.add(new WelComeData("艺术壁纸", 10));

		list.add(new WelComeData("风景壁纸", 12));

		list.add(new WelComeData("非主流壁纸", 14));
		
		
		
		list.add(new WelComeData("明星壁纸", 2));
		
		
//		list.add(new WelComeData("未分类", 4));
		
		list.add(new WelComeData("影片壁纸", 6));
		
//		list.add(new WelComeData("未分类", 8));
		
		list.add(new WelComeData("体育壁纸", 9));
		
//		list.add(new WelComeData("未分类", 16));
		
		list.add(new WelComeData("游戏壁纸", 21));
		
//		list.add(new WelComeData("未分类", 24));
		

		return list;

	}

	private class GridViewAdapter extends BaseAdapter
	{

		public GridViewAdapter(Context c, List<WelComeData> list)
		{
			mContext = c;
			urls = list;
		}

		private Context mContext;
		private List<WelComeData> urls;

		@Override
		public int getCount()
		{
			return urls.size();
		}

		@Override
		public Object getItem(int position)
		{
			return urls.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{

			Button button = (Button) convertView;

			if (null == button)
			{
				button = new Button(mContext);
			}
//			button.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, 80));
			
			button.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, 50));
			button.setClickable(false);
			button.setFocusable(false);
			button.setText(urls.get(position).name);
			return button;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		// TODO Auto-generated method stub
	
		if(keyCode == KeyEvent.KEYCODE_BACK){
			
			new AlertDialog.Builder(Welcome.this).setTitle("退出程序").setMessage(
				"确定退出程序吗？").setPositiveButton("是", new OnClickListener()
			{

				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					Welcome.this.finish();
				}
			}).setCancelable(false).setNegativeButton("否", null).create().show();
			
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private class WelComeData
	{

		public WelComeData(String name, int index)
		{

			this.name = name;
			this.index = index;
		}

		public String name;
		public int index;
	}

	@Override
	protected void onDestroy()
	{
		// clearCache(getCacheDir().getParentFile());

		clearCache(getCacheDir());
		super.onDestroy();
	}

	private void clearCache(File file)
	{
		if (null == file)
		{
			return;
		}

		if (file.isDirectory())
		{
			File[] listFiles = file.listFiles();
			for (File f : listFiles)
			{
				clearCache(f);
			}
			file.delete();
		}
		else
		{
			if (file.exists())
				file.delete();
		}
	}

}